package com.xfp.controller;

import com.xfp.pojo.dto.PartnerDto;
import com.xfp.pojo.dto.PartnerQueryDto;
import com.xfp.pojo.vo.PartnerQueryVo;
import com.xfp.pojo.vo.PartnerVo;
import com.xfp.result.Result;
import com.xfp.service.PartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user-service/partner")
@Slf4j
@Api(tags = "合作商")
public class PartnerController {

    @Autowired
    private PartnerService partnerService;

    /**
     * 新增合作商
     * @param dto
     * @return
     * @throws Exception
     */
    @PostMapping
    @ApiOperation("新增合作商")
    public Result<Boolean> insert(@RequestBody PartnerDto dto) throws Exception {
        log.info("新增合作商：{}",dto);
        partnerService.insert(dto);
        return Result.success(true);
    }

    /**
     * 分页查询合作商
     * @param dto
     * @return
     * @throws Exception
     */
    @GetMapping("/search")
    @ApiOperation("分页查询合作商")
    public PartnerQueryVo page(PartnerQueryDto dto) throws Exception {
        log.info("分页查询合作商：{}",dto);
        PartnerQueryVo vo = partnerService.page(dto);
        return vo;
    }

    /**
     * 修改合作商
     * @param id
     * @param dto
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation("修改合作商")
    public Result<Boolean> update(@PathVariable Integer id,@RequestBody PartnerDto dto) throws Exception {
        log.info("修改合作商：{},{}",id,dto);
        partnerService.update(id,dto);
        return Result.success(true);
    }

    /**
     * 获取合作商信息
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/{id}")
    @ApiOperation("获取合作商基本信息")
    public PartnerVo findById(@PathVariable Integer id) throws Exception {
        log.info("获取合作商基本信息：{}",id);
        PartnerVo vo = partnerService.findById(id);
        return vo;
    }

    /**
     * 删除合作商
     * @param id
     * @return
     * @throws Exception
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除合作商")
    public Result<Boolean> delete(@PathVariable Integer id) throws Exception {
        log.info("删除合作商：{}",id);
        partnerService.delete(id);
        return Result.success(true);
    }

    /**
     * 重置合作商密码
     */
    @PutMapping("/resetPwd/{id}")
    @ApiOperation("重置合作商密码")
    public void restPwd(@PathVariable Integer id){
        log.info("重置合作商密码");
        partnerService.restPwd(id);
    }
}
